home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14555 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  55 lines

  1. Path: ix.netcom.com!news
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: MFC CArray Template
  5. Date: 30 Mar 1996 23:17:36 GMT
  6. Organization: Netcom
  7. Message-ID: <4jkfeg$i4b@dfw-ixnews6.ix.netcom.com>
  8. References: <315C1AAD.725@garlic.com>
  9. NNTP-Posting-Host: den-co15-25.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Sat Mar 30  5:17:36 PM CST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <315C1AAD.725@garlic.com>, grant@garlic.com says...
  16. >
  17. >In MSVC 1.52, I noticed that the CArray template uses the memcpy and 
  18. >memmove functions rather than the contained object's assignment and copy 
  19. >functions.  Will this work for any object?  I thought that doing a 
  20. >bit-wise copy of an object wreaks havoc to the copy.
  21.  
  22. And you are right (does MFC *really* do this?!?!?).
  23. For example, consider a string class that may either allocate from the heap or 
  24. use an internal array for small strings:
  25.  
  26. class Str {
  27.     Str(char *s);
  28. private:
  29.     char *ptr;
  30.     char smallArray[20];
  31. }
  32.  
  33. Str::Str(char*s) : ptr(0)
  34. {
  35.    if (strlen(s)+1 > sizeof(smallArray))
  36.    {
  37.       ptr = new char[strlen(s)+1];
  38.    } else {
  39.       ptr = smallArray;
  40.    }
  41.    strcpy(ptr, s);
  42. }
  43.  
  44. Clearly, a bitwise-copy of a Str in the "small" mode will fail.
  45.  
  46. >What advantages are there to implementing CArray this way?
  47.  
  48. 1) Speed.
  49. 2) Satisfaction of sadistic tendencies?
  50.  
  51. IMHO, use STL or tools.h++
  52.  
  53. john lilley
  54.  
  55.